package com.study.tiger.config;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * @Author: liutingting
 * @Date: 2022/3/4 000411:04
 * @Version: 1.0
 */
@Slf4j
@AllArgsConstructor
public class RedisKeySerializer implements RedisSerializer<String> {
    //    private final String redisKeyPrefix;
    private final Charset charset;

    RedisKeySerializer() {
        this.charset = StandardCharsets.UTF_8;
    }


    @Override
    public String deserialize(byte[] bytes) {
        if (null == bytes){
            return null;
        }
        String saveKey = new String(bytes, charset);

        return saveKey;
    }

    @Override
    public byte[] serialize(String string) {
        if (null == string){
            return null;
        }
        String key = string;
        return key.getBytes(charset);
    }
}
